package com.zyl.familyspaceauth.shiro.matcher;
/*
 * Project: am
 *
 * File Created at 2021/7/14
 *
 * Copyright 2016 Corporation Limited. All rights reserved.
 *
 * This software is the confidential and proprietary information of Company. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in accordance with the terms of the license.
 */

import com.zyl.familyspacecommoncore.PasswordUtils;
import com.zyl.familyspacecommoncore.token.AccountToken;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.springframework.stereotype.Component;

/**
 * @author zhouleibin
 * @Desc 类通过自定义密码匹配逻辑，确保用户输入的密码在经过特定的加密操作后与系统存储的加密密码相匹配。如果匹配成功，用户可以成功登录，否则登录会被拒绝。
 * 这个类通常在用户登录时被调用，用于验证用户的身份，确保用户提供的密码是正确的。
 * @date 2021/7/14 12:43
 */
@Component
public class PasswordMatcher extends SimpleCredentialsMatcher {
    /**
     * 先实现一个接口SimpleCredentialsMatcher 获取doCredentialsMatch的内部方法
     *
     * @param token 通过token 获取用户密码，并转换成String类型，注意这里的转换不能强转，要用new的方法
     * @param info  通过AuthenticationInfo 的info 查询数据库的密码 装换成String类型
     * @return Result
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        AccountToken accountToken = (AccountToken) token;
        String encryptionPassword = PasswordUtils.getEncryptionPassword(accountToken.getSalt(),
                new String(accountToken.getPassword()));
        String credentials = (String) info.getCredentials();
        return super.equals(encryptionPassword, credentials);
    }
}
